package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserLoginVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "车辆管理")
@RestController
@RequestMapping("v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session) {
        log.debug("车辆信息---" + vehicleAddParam);
        UserLoginVO userLoginVO = (UserLoginVO) session.getAttribute("user");
        if (userLoginVO != null) {
            Vehicle vehicle = new Vehicle();
            BeanUtils.copyProperties(vehicleAddParam, vehicle);
            Long userId =userLoginVO.getId();
            log.debug("用户ID---" + userId);
            vehicle.setUserId(userId);
            int i = vehicleMapper.addVehicle(vehicle);
            log.debug("受影响行数---" + i);
            if (i > 0) {
                return new JsonResult(StatusCode.OPERATION_SUCCESS);
            }
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }
        return new JsonResult(StatusCode.NOT_LOGIN);
    }

    @GetMapping("list")
    @Operation(summary = "车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameter(name = "brand", description = "车辆品牌")
    @Parameter(name = "license", description = "车辆车牌")
    public JsonResult list(VehicleListQuery vehicleListQuery, HttpSession session) {
//        UserLoginVO userLoginVO = (UserLoginVO) session.getAttribute("user");
//        if (userLoginVO != null) {
            List<Vehicle> list = vehicleMapper.list(vehicleListQuery);
            return JsonResult.ok(list);
//        }
//        return new JsonResult(StatusCode.NOT_LOGIN);
    }

    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id", description = "车辆ID" )
    public JsonResult del(@PathVariable Long id, HttpSession session) {
        UserLoginVO userLoginVO = (UserLoginVO) session.getAttribute("user");
        if (userLoginVO != null) {
            int i = vehicleMapper.del(id);
            if (i > 0) {
                return new JsonResult(StatusCode.OPERATION_SUCCESS);
            }
        }
        return new JsonResult(StatusCode.NOT_LOGIN);
    }
}
